package com.zhuannimei.adapter;

import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.R.integer;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.zhuannimei.R;
import com.zhuannimei.dialog.BaseDialog;
import com.zhuannimei.domain.Product;
import com.zhuannimei.net.RequestServerData;
import com.zhuannimei.ui.TabTwoActivity;
import com.zhuannimei.utility.JackViewHolder;
import com.zhuannimei.utility.TextHelper;
import com.zhuannimei.utility.UrlBank;
import com.zhuannimei.utility.WuLog;
import com.zhuannimei.utility.ZnmData;
import com.zhuannimei.utility.ZnmHttpParams;

/**
 * 商品列表适配器
 * 
 * @author Jack 2013-12-4
 * 
 * 
 */
public class ProductAdapter extends BaseObjectListAdapter {
	private BaseDialog mBackDialog;
	public ProductAdapter(Context context, List<? extends Object> datas) {
		super(context, datas);
		mBackDialog = new BaseDialog(mContext);
		mBackDialog.setTitle("确定兑换");
		mBackDialog.setCancelable(true);
		mBackDialog.setCanceledOnTouchOutside(true);

	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.tab_product, null);
		}

		final Product pro = (Product) mDatas.get(position);
		ImageView proPic = JackViewHolder.get(convertView,
				R.id.feed_item_iv_avatar);
		imageLoader.displayImage(UrlBank.getServerIp() + pro.getPhoto(),
				proPic, options);
		TextView proName = JackViewHolder.get(convertView,
				R.id.feed_item_htv_name);
		TextView proDes = JackViewHolder.get(convertView,
				R.id.feed_item_etv_content);
		TextView proTime = JackViewHolder.get(convertView,
				R.id.feed_item_htv_time);
		TextView proBuyed = JackViewHolder.get(convertView,
				R.id.product_price_tv);
		TextView proPrice = JackViewHolder.get(convertView,
				R.id.product_yuan_tv);
		ImageView proBuy = JackViewHolder.get(convertView, R.id.product_iv_buy);
		TextView proNum = JackViewHolder.get(convertView, R.id.product_num_tv);
		// 更换背景
		RelativeLayout rl_root = JackViewHolder.get(convertView,
				R.id.feed_item_layout_root);
		ImageView pro_empty = JackViewHolder.get(convertView,
				R.id.product_empty_iv);
		if ("0".equals(pro.getLast())) {
			// 已经被抢光了
			rl_root.setBackgroundResource(R.drawable.bg_card_over);
			pro_empty.setVisibility(View.VISIBLE);
			proBuy.setVisibility(View.GONE);
		} else {
			proBuy.setVisibility(View.VISIBLE);
			pro_empty.setVisibility(View.GONE);
			rl_root.setBackgroundResource(R.drawable.bg_card_cmcc);
		}
		proPrice.setText("剩余数量:" + pro.getLast() + "个");
		proNum.setText("市场价/￥:" + pro.getPrice());
		proBuyed.setText("" + pro.getBuyed() + "妹币/1人次");
		proTime.setText(TextHelper.shortTime(pro.getCtime()) );
		proDes.setText("商品描述:" + pro.getDescribe());
		proName.setText("" + pro.getName());

		proBuy.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// getProductsFromNet(ZnmData.getUserData().getUserUserId(),
				// pro.getId(), 1+"",pro);
				mBackDialog.setMessage("您确定要花费" + pro.getBuyed() + "妹币购买吗？");
				mBackDialog.setButton1("确定",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								getProductsFromNet(ZnmData.getUserData()
										.getUserUserId(), pro.getId(), 1 + "",
										pro);
								mBackDialog.dismiss();
							}
						});
				mBackDialog.setButton2("取消",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								mBackDialog.cancel();
							}

						});
				mBackDialog.show();
			}
		});

		return convertView;
	}

	/**
	 * 点击后购买商品
	 */
	private void getProductsFromNet(String userId, String productId,
			String number, final Product pro) {
		Toast.makeText(mContext, "正在购买……", 0).show();
		RequestParams mParams = ZnmHttpParams.getDefaultRequestParams();
		mParams.addBodyParameter("userId", userId + "");
		mParams.addBodyParameter("productId", productId + "");
		mParams.addBodyParameter("number", number + "");
		RequestServerData.getProductsFromNet(mParams,
				new RequestCallBack<String>() {

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						String results = responseInfo.result;
						try {
							JSONObject jo = new JSONObject(results);
							String code = jo.optString("code");
							if ("0".equals(code)) {
								String num = pro.getLast();
								int nn = Integer.parseInt(num);
								if (nn != 0) {
									int nnn = nn - 1;
									pro.setLast(nnn + "");
								}
								notifyDataSetChanged();
								Toast.makeText(mContext, "购买成功,请等待审核……", 0)
										.show();
							} else {
								Toast.makeText(mContext, "购买失败,请重新购买……", 0)
										.show();
							}
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						Toast.makeText(mContext, "购买失败,请重新购买……", 0).show();
						notifyDataSetChanged();
					}
				});
	}
}
